package com.weme.live.view
import android.content.Context
import android.graphics.*
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import androidx.core.content.res.ResourcesCompat
import com.weme.live.R

class LiveProgressBar(context: Context, attrs: AttributeSet? = null) :
    View(context, attrs) {
    private val MAX_PROGRESS = 100f
    private val MAX_ANGLE = 360f
    private lateinit var mBackgroudPaint: Paint
     lateinit var mClipRoundBackgroundPaint: Paint
    private lateinit var mClipRoundImagePaint: Paint
    private lateinit var mRoundProgressPaint: Paint
    private var mClipRoundBackgroundColor = 0
    private lateinit var mTextPaint: Paint
    private var mBackRoundWidth = 8f
    private var mRoundWidth = 8f
    private var mBackRoundColor: Int = 0
    private val mRoundRectF = RectF()
    private var mMinLenght = 1000
    private var mRoundPath = Path()
    private var mProgress = 10f
    private var mText = ""
    private var mTextSize = 0f
    val customTypeface :Typeface?
    private var mInnerBackgroundColor = Color.parseColor("#4D000000") // 替换成您需要的颜色


    init {
        initView(context, attrs)
         customTypeface = ResourcesCompat.getFont(context, com.example.resources.R.font.book)
    }

    private fun initView(context: Context, attrs: AttributeSet?) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar)
        mText = typedArray.getString(R.styleable.RoundProgressBar_text) ?: ""
        mTextSize = typedArray.getDimensionPixelSize(R.styleable.RoundProgressBar_textSize, 0).toFloat()
        mBackRoundWidth = typedArray.getDimensionPixelSize(R.styleable.RoundProgressBar_backRoundWidth, 16).toFloat()
        mRoundWidth = typedArray.getDimensionPixelSize(R.styleable.RoundProgressBar_roundWidth, 16).toFloat()

        typedArray.recycle()

        mBackgroudPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mBackgroudPaint.style = Paint.Style.STROKE
        mBackgroudPaint.strokeWidth = mBackRoundWidth
        mBackRoundColor = Color.parseColor("#4D000000")
        mBackgroudPaint.color = mBackRoundColor

        mClipRoundImagePaint = Paint(Paint.ANTI_ALIAS_FLAG)

        mRoundProgressPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mRoundProgressPaint.style = Paint.Style.STROKE
        mRoundProgressPaint.color = Color.parseColor("#FAFF0D")
        mRoundProgressPaint.strokeWidth = mRoundWidth

        mTextPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mTextPaint.color = Color.WHITE
        mTextPaint.textSize = 14f
        mTextPaint.style = Paint.Style.FILL

        mRoundPath = Path()


        mClipRoundBackgroundPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mClipRoundBackgroundPaint.style = Paint.Style.FILL
        mClipRoundBackgroundColor = Color.parseColor("#1AFFFFFF")
        mClipRoundBackgroundPaint.color = mClipRoundBackgroundColor


        mInnerBackgroundColorPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mInnerBackgroundColorPaint.color = mInnerBackgroundColor
        mInnerBackgroundColorPaint.style = Paint.Style.FILL
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val width = MeasureSpec.getSize(widthMeasureSpec)
        val height = MeasureSpec.getSize(heightMeasureSpec)
        if (width != height) {
            mMinLenght = Math.min(width, height)
            setMeasuredDimension(mMinLenght, mMinLenght)
        } else {
            mMinLenght = width
        }
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mRoundPath.addCircle(mMinLenght / 2.toFloat(), mMinLenght / 2.toFloat(), ((mMinLenght - mBackRoundWidth * 2) / 2).toFloat(), Path.Direction.CW)
//        if (mBitmap != null) {
//            mBitmap = circleBitmap(mBitmap!!)
//        }
    }
    private lateinit var mInnerBackgroundColorPaint: Paint



    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        // 绘制内圈背景色
        canvas.drawCircle(
            mMinLenght / 2.toFloat(),
            mMinLenght / 2.toFloat(),
            (mMinLenght / 2 - mBackRoundWidth / 2 - mRoundWidth / 2), // 内圈半径减去内边距和外边距
            mInnerBackgroundColorPaint
        )

//        // 绘制内圈
//        canvas.drawCircle(
//            mMinLenght / 2.toFloat(),
//            mMinLenght / 2.toFloat(),
//            (mMinLenght / 2 - mBackRoundWidth / 2 - mRoundWidth / 2), // 内圈半径减去内边距和外边距
//            mRoundProgressPaint
//        )


        canvas.drawCircle(mMinLenght / 2.toFloat(), mMinLenght / 2.toFloat(), (mMinLenght / 2 - mBackRoundWidth / 2), mBackgroudPaint)

            canvas.drawCircle(
                mMinLenght / 2.toFloat(),
                mMinLenght / 2.toFloat(),
                (mMinLenght / 2 - mBackRoundWidth / 2 - mRoundWidth / 2), // 内圈半径减去内边距和外边距
                mClipRoundBackgroundPaint
            )

        mRoundRectF.set(
            mBackRoundWidth + mRoundWidth / 2,
            mBackRoundWidth + mRoundWidth / 2,
            mMinLenght - (mBackRoundWidth + mRoundWidth / 2),
            mMinLenght - (mBackRoundWidth + mRoundWidth / 2)
        )
        canvas.drawArc(mRoundRectF, 270f, mProgress / MAX_PROGRESS * MAX_ANGLE, false, mRoundProgressPaint)
        if (mText != null&&!TextUtils.isEmpty(mText)) {

            mTextPaint.typeface = customTypeface
            drawTextWithCenterPoint(canvas, mMinLenght / 2, mMinLenght / 2, (25-(Math.max(mProgress, 0f)/4)).toInt().toString(), mTextPaint)
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()

    }

    fun setProgress(progress: Float) {
        mProgress = progress.toFloat()
        if (mProgress > 100) {
            mProgress = 100f
        }
        invalidate()
    }

    fun setText(text: String?) {
        mText = text ?: ""
        invalidate()
    }

    private fun drawTextWithCenterPoint(canvas: Canvas, centerX: Int, centerY: Int, text: String, paint: Paint) {
        val textWidth = paint.measureText(text)
        val fontMetrics = paint.fontMetrics
        val baselineY = centerY + (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom
        canvas.drawText(text, centerX - textWidth / 2, baselineY, paint)
    }

    private fun circleBitmap(source: Bitmap): Bitmap {
        val width = source.width
        val bitmap = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        val paint = Paint()
        paint.isAntiAlias = true
        canvas.clipPath(mRoundPath)
        canvas.drawBitmap(source, 0f, 0f, paint)
        return bitmap
    }
}
